import EventManager from "../core/EventManager";
import LanguageManager from "../core/LanguageManager";
import ResManager from "../core/ResManager";
import UIBase from "../core/UIBase";
import GuideData from "../data/GuideData";
import { CommonEvents } from "../net/CommonEvents";
import LogUtil from "../utils/LogUtil";
import NodeUtil from "../utils/NodeUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIGuide extends UIBase {

    private guideTalk: cc.Node = null;
    private guideArrow: cc.Node = null;
    private guideFinger: cc.Node = null;
    private mask: cc.Node = null;
    private mask2: cc.Node = null;
    private maskLight: cc.Node = null;
    private passNode: cc.Node = null;
    private eventNode: cc.Node = null;
    private lbTalk:cc.Label = null;
    private fingerAnim:cc.Animation = null;
    

    private maskCtrl: cc.Mask = null;
    private config: any = null;
    private restartTime: number = 0;

    onLoad(){
        // super.onLoad();
        this.guideTalk = NodeUtil.getChildByName(this.node,'guide_talk');
        this.guideArrow = NodeUtil.getChildByName(this.node,'guide_arrow');
        this.guideFinger = NodeUtil.getChildByName(this.node,'guide_finger');
        this.mask = NodeUtil.getChildByName(this.node,'mask');
        this.mask2 = NodeUtil.getChildByName(this.node,'mask2');
        this.maskLight = NodeUtil.getChildByName(this.node,'guideLight');
        this.passNode = NodeUtil.getChildByName(this.node,'BtnPass');
        this.eventNode = NodeUtil.getChildByName(this.node,'EventOcclusion');

        this.lbTalk = NodeUtil.getChildByName(this.guideTalk,'lbTalk',cc.RichText);
        this.fingerAnim = NodeUtil.getChildByName(this.guideFinger,'anim',cc.Animation);

        this.maskCtrl = this.mask.getComponent(cc.Mask)

        this.eventNode.on(cc.Node.EventType.TOUCH_START, this.CanTouch, this);
        this.eventNode.on(cc.Node.EventType.TOUCH_END, this.CanEnd, this);

        EventManager.on(CommonEvents.GUIDE_CLOSE,this.closeGuide,this);
    }

    onDestroy() {
        super.onDestroy();
        EventManager.off(CommonEvents.GUIDE_CLOSE,this.closeGuide,this);
    }

    CanTouch(event:any) {
        let point = event.getLocation();
        let location = this.node.parent.convertToNodeSpaceAR(point);
        let retWord = this.mask.getBoundingBox();
        if (retWord.contains(location)) {
            this.eventNode._touchListener.setSwallowTouches(false);
        } else {
            this.eventNode._touchListener.setSwallowTouches(true);
        }
    }

    CanEnd(event:any) {
        let point = event.getLocation();
        let location = this.node.parent.convertToNodeSpaceAR(point);
        let retWord = this.mask.getBoundingBox();
        if (retWord.contains(location)) {
            this.eventNode._touchListener.setSwallowTouches(false);
        } else {
            if (this.config) {
                if (this.config.completeWay == 200 || this.config.completeWay == 201) {
                    this.eventNode._touchListener.setSwallowTouches(false);
                    return
                }
                GuideData.ins().complete(-1);
            }
            this.eventNode._touchListener.setSwallowTouches(true);
        }
    }

    init(options) {
        this.node.active = true;
        this.passNode.active = false;//跳过按钮
        this.guideFinger.active = false;//手指
        this.guideArrow.active = false;//滑动箭头
        this.guideTalk.active = false;//主角废话

        this.mask.position = cc.v3(0, 0, 0);
        this.mask.width = 0;
        this.mask.height = 0;
        this.mask2.position = cc.v3(0, 0, 0);
        this.mask2.width = 0;
        this.mask2.height = 0;
        this.maskLight.position = cc.v3(0, 0, 0);
        this.maskLight.width = 0;
        this.maskLight.height = 0;

        this.restartTime = 0
        this.scheduleOnce(() => {
            this.initView(options)
        }, 0.2);
    }

    initView(options){
        let groupId = options.groupId;
        let smallId = options.smallId;
        let config_guide:any = ResManager.ins().getRes('config_guide');
        let config = config_guide[groupId][smallId];
        if (config) {
            this.config = config;
        }else{
            LogUtil.warn('引导配置未找到！groupId, smallId>>',groupId, smallId);
            this.hide();
            return
        }
        LogUtil.log('config>>',config);

        if(config.lightNode){
            let baseNode = cc.Canvas.instance.node;
            let lightNode = NodeUtil.getChildByName(baseNode,config.lightNode);
            if (lightNode) {
                let pos = this.node.parent.convertToNodeSpaceAR(lightNode.parent.convertToWorldSpaceAR(lightNode.position));
                //获得当前缩放比例
                let scale = lightNode.getBoundingBoxToWorld();
                this.mask.position = cc.v3(pos.x, pos.y, 0);
                this.mask.width = scale.width;
                this.mask.height = scale.height;
                this.mask2.position = cc.v3(pos.x, pos.y, 0);
                this.mask2.width = scale.width + 50;
                this.mask2.height = scale.height + 50;
                this.maskLight.position = cc.v3(pos.x, pos.y, 0);
                this.maskLight.width = scale.width + 50;
                this.maskLight.height = scale.height + 50;
                
                //手指
                this.guideFinger.active = true;
                this.fingerAnim.play();
                if (config.fingerPosition) {
                    this.guideFinger.position = cc.v3(config.fingerPosition.id, config.fingerPosition.count, 0);
                } else {
                    this.guideFinger.position = this.mask.position;
                }
                if (this.guideFinger.position.x < 0 && this.guideFinger.position.y < 0) {
                    this.guideFinger.angle = 90;
                } else if (this.guideFinger.position.x > 0 && this.guideFinger.position.y < 0) {
                    this.guideFinger.angle = 180;
                } else if (this.guideFinger.position.x > 0 && this.guideFinger.position.y > 0) {
                    this.guideFinger.angle = -90;
                } else {
                    this.guideFinger.angle = 0;
                }
            }else{
                this.restartTime++;
                LogUtil.warn('找不到高亮对应的节点！！！！！', config.ID, config.lightNode,this.restartTime);
                if (this.restartTime < 5) {
                    this.scheduleOnce(() => {
                        this.initView(options);
                    }, 0.2)
                } else {
                    this.passNode.active = true;
                }
                return;
            }
        } else {//全黑
            this.mask.position = cc.v3(0, 0, 0);
            this.mask.width = 0;
            this.mask.height = 0;
            this.mask2.position = cc.v3(0, 0, 0);
            this.mask2.width = 0;
            this.mask2.height = 0;
            this.maskLight.position = cc.v3(0, 0, 0);
            this.maskLight.width = 0;
            this.maskLight.height = 0;
        }

        //指引精灵对话
        if (config.guideDes && config.desPosition) {
            this.guideTalk.active = true;
            this.lbTalk.string = LanguageManager.ins().getString(config.guideDes);
            this.guideTalk.position = cc.v3(config.desPosition.id, config.desPosition.count, 0);
        }

        //跳过引导按钮
        this.passNode.active = config.canPass ? true : false;
    }
    
    onPassClick(){
        GuideData.ins().pass();
    }

    closeGuide(){
        this.hide();
    }

    hide() {
        this.node.active = false;
    }
}
